package com.sky.skyopsserver.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.sky.skyopsserver.aspect.ApiOperationLog;
import com.sky.skyopsserver.service.HostGroupService;
import com.sky.skyopsserver.utils.PageResponse;
import com.sky.skyopsserver.utils.Response;
import com.sky.skyopsserver.vos.*;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import jakarta.validation.constraints.NotEmpty;
import jakarta.validation.constraints.NotNull;
import jakarta.validation.constraints.Positive;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 主机组信息控制器
 * 
 * @author Administrator
 * @since 2025-01-17
 */
@Slf4j
@RestController
@RequestMapping("/api/host-group")
@RequiredArgsConstructor
@Validated
@Tag(name = "主机组管理", description = "主机组信息的增删改查操作")
public class HostGroupController {

    private final HostGroupService hostGroupService;

    /**
     * 添加主机组
     * 
     * @param reqVO 添加主机组请求VO
     * @return 主机组ID
     */
    @PostMapping("/add")
    @Operation(summary = "添加主机组", description = "新增一个主机组信息")
    @ApiOperationLog(description = "添加主机组")
    public Response<Long> addHostGroup(@Valid @RequestBody HostGroupAddReqVO reqVO) {
        Long groupId = hostGroupService.addHostGroup(reqVO);
        return Response.success(groupId);
    }

    /**
     * 更新主机组信息
     * 
     * @param reqVO 更新主机组请求VO
     * @return 操作结果
     */
    @PutMapping("/update")
    @Operation(summary = "更新主机组信息", description = "修改主机组的基本信息")
    @ApiOperationLog(description = "更新主机组信息")
    public Response<Boolean> updateHostGroup(@Valid @RequestBody HostGroupUpdateReqVO reqVO) {
        Boolean result = hostGroupService.updateHostGroup(reqVO);
        return Response.success(result);
    }

    /**
     * 删除主机组
     * 
     * @param id 主机组ID
     * @return 操作结果
     */
    @DeleteMapping("/delete/{id}")
    @Operation(summary = "删除主机组", description = "根据ID删除指定主机组")
    @ApiOperationLog(description = "删除主机组")
    public Response<Boolean> deleteHostGroup(
            @Parameter(description = "主机组ID", required = true)
            @PathVariable("id") @NotNull(message = "主机组ID不能为空") @Positive(message = "主机组ID必须为正数") Long id) {
        Boolean result = hostGroupService.deleteHostGroup(id);
        return Response.success(result);
    }

    /**
     * 批量删除主机组
     * 
     * @param ids 主机组ID列表
     * @return 操作结果
     */
    @DeleteMapping("/batch-delete")
    @Operation(summary = "批量删除主机组", description = "根据ID列表批量删除主机组")
    @ApiOperationLog(description = "批量删除主机组")
    public Response<Boolean> batchDeleteHostGroup(
            @Parameter(description = "主机组ID列表", required = true)
            @RequestBody @NotEmpty(message = "主机组ID列表不能为空") List<@Positive(message = "主机组ID必须为正数") Long> ids) {
        Boolean result = hostGroupService.batchDeleteHostGroup(ids);
        return Response.success(result);
    }

    /**
     * 根据ID查询主机组详情
     * 
     * @param id 主机组ID
     * @return 主机组详情
     */
    @GetMapping("/detail/{id}")
    @Operation(summary = "查询主机组详情", description = "根据ID获取主机组的详细信息")
    @ApiOperationLog(description = "查询主机组详情")
    public Response<HostGroupRspVO> getHostGroupById(
            @Parameter(description = "主机组ID", required = true)
            @PathVariable("id") @NotNull(message = "主机组ID不能为空") @Positive(message = "主机组ID必须为正数") Long id) {
        HostGroupRspVO result = hostGroupService.getHostGroupById(id);
        return Response.success(result);
    }

    /**
     * 根据ID查询主机组详情（包含主机列表）
     * 
     * @param id 主机组ID
     * @return 主机组详情（包含主机列表）
     */
    @GetMapping("/detail-with-hosts/{id}")
    @Operation(summary = "查询主机组详情（含主机列表）", description = "根据ID获取主机组的详细信息，包含主机列表")
    @ApiOperationLog(description = "查询主机组详情（含主机列表）")
    public Response<HostGroupDetailRspVO> getHostGroupDetailById(
            @Parameter(description = "主机组ID", required = true)
            @PathVariable("id") @NotNull(message = "主机组ID不能为空") @Positive(message = "主机组ID必须为正数") Long id) {
        HostGroupDetailRspVO result = hostGroupService.getHostGroupDetailById(id);
        return Response.success(result);
    }

    /**
     * 分页查询主机组列表
     * 
     * @param reqVO 分页查询请求VO
     * @return 分页结果
     */
    @PostMapping("/page")
    @Operation(summary = "分页查询主机组列表", description = "根据条件分页查询主机组信息")
    @ApiOperationLog(description = "分页查询主机组列表")
    public PageResponse<HostGroupRspVO> getHostGroupPage(@Valid @RequestBody HostGroupPageReqVO reqVO) {
        IPage<HostGroupRspVO> page = hostGroupService.getHostGroupPage(reqVO);
        return PageResponse.success(page, page.getRecords());
    }

    /**
     * 查询所有主机组选项（用于下拉选择）
     * 
     * @return 主机组选项列表
     */
    @GetMapping("/options")
    @Operation(summary = "查询主机组选项", description = "获取所有主机组的选项列表，用于下拉选择")
    @ApiOperationLog(description = "查询主机组选项")
    public Response<List<HostGroupOptionRspVO>> getAllHostGroupOptions() {
        List<HostGroupOptionRspVO> result = hostGroupService.getAllHostGroupOptions();
        return Response.success(result);
    }

    /**
     * 查询有主机的主机组选项
     * 
     * @return 主机组选项列表
     */
    @GetMapping("/options/with-hosts")
    @Operation(summary = "查询有主机的主机组选项", description = "获取包含主机的主机组选项列表")
    @ApiOperationLog(description = "查询有主机的主机组选项")
    public Response<List<HostGroupOptionRspVO>> getHostGroupOptionsWithHosts() {
        List<HostGroupOptionRspVO> result = hostGroupService.getHostGroupOptionsWithHosts();
        return Response.success(result);
    }

    /**
     * 查询空主机组选项
     * 
     * @return 主机组选项列表
     */
    @GetMapping("/options/empty")
    @Operation(summary = "查询空主机组选项", description = "获取不包含主机的主机组选项列表")
    @ApiOperationLog(description = "查询空主机组选项")
    public Response<List<HostGroupOptionRspVO>> getEmptyHostGroupOptions() {
        List<HostGroupOptionRspVO> result = hostGroupService.getEmptyHostGroupOptions();
        return Response.success(result);
    }

    /**
     * 获取主机组统计信息
     * 
     * @return 主机组统计信息
     */
    @GetMapping("/statistics")
    @Operation(summary = "获取主机组统计信息", description = "获取主机组的各项统计数据")
    @ApiOperationLog(description = "获取主机组统计信息")
    public Response<HostGroupStatisticsRspVO> getHostGroupStatistics() {
        HostGroupStatisticsRspVO result = hostGroupService.getHostGroupStatistics();
        return Response.success(result);
    }

    /**
     * 检查主机组名称是否已存在
     * 
     * @param name 主机组名称
     * @param excludeId 排除的主机组ID（可选）
     * @return 是否存在
     */
    @GetMapping("/check-name")
    @Operation(summary = "检查主机组名称", description = "检查主机组名称是否已存在")
    @ApiOperationLog(description = "检查主机组名称")
    public Response<Boolean> checkHostGroupNameExists(
            @Parameter(description = "主机组名称", required = true)
            @RequestParam("name") String name,
            @Parameter(description = "排除的主机组ID")
            @RequestParam(value = "excludeId", required = false) Long excludeId) {
        Boolean result = hostGroupService.checkHostGroupNameExists(name, excludeId);
        return Response.success(result);
    }

    /**
     * 根据名称模糊查询主机组
     * 
     * @param name 主机组名称关键字
     * @return 主机组列表
     */
    @GetMapping("/search")
    @Operation(summary = "搜索主机组", description = "根据主机组名称关键字模糊查询主机组")
    @ApiOperationLog(description = "搜索主机组")
    public Response<List<HostGroupRspVO>> searchHostGroupByName(
            @Parameter(description = "主机组名称关键字", required = true)
            @RequestParam("name") String name) {
        List<HostGroupRspVO> result = hostGroupService.searchHostGroupByName(name);
        return Response.success(result);
    }

    /**
     * 移动主机到指定主机组
     * 
     * @param hostIds 主机ID列表
     * @param targetGroupId 目标主机组ID
     * @return 操作结果
     */
    @PostMapping("/move-hosts")
    @Operation(summary = "移动主机到主机组", description = "将指定主机移动到目标主机组")
    @ApiOperationLog(description = "移动主机到主机组")
    public Response<Boolean> moveHostsToGroup(
            @Parameter(description = "主机ID列表", required = true)
            @RequestParam("hostIds") @NotEmpty(message = "主机ID列表不能为空") List<@Positive(message = "主机ID必须为正数") Long> hostIds,
            @Parameter(description = "目标主机组ID", required = true)
            @RequestParam("targetGroupId") @NotNull(message = "目标主机组ID不能为空") @Positive(message = "目标主机组ID必须为正数") Long targetGroupId) {
        Boolean result = hostGroupService.moveHostsToGroup(hostIds, targetGroupId);
        return Response.success(result);
    }

    /**
     * 从主机组中移除主机
     * 
     * @param hostIds 主机ID列表
     * @param groupId 主机组ID
     * @return 操作结果
     */
    @PostMapping("/remove-hosts")
    @Operation(summary = "从主机组移除主机", description = "从指定主机组中移除主机")
    @ApiOperationLog(description = "从主机组移除主机")
    public Response<Boolean> removeHostsFromGroup(
            @Parameter(description = "主机ID列表", required = true)
            @RequestParam("hostIds") @NotEmpty(message = "主机ID列表不能为空") List<@Positive(message = "主机ID必须为正数") Long> hostIds,
            @Parameter(description = "主机组ID", required = true)
            @RequestParam("groupId") @NotNull(message = "主机组ID不能为空") @Positive(message = "主机组ID必须为正数") Long groupId) {
        Boolean result = hostGroupService.removeHostsFromGroup(hostIds, groupId);
        return Response.success(result);
    }

    /**
     * 获取主机组下的主机数量
     * 
     * @param groupId 主机组ID
     * @return 主机数量
     */
    @GetMapping("/host-count/{groupId}")
    @Operation(summary = "获取主机组主机数量", description = "获取指定主机组下的主机数量")
    @ApiOperationLog(description = "获取主机组主机数量")
    public Response<Long> getHostCountByGroupId(
            @Parameter(description = "主机组ID", required = true)
            @PathVariable("groupId") @NotNull(message = "主机组ID不能为空") @Positive(message = "主机组ID必须为正数") Long groupId) {
        Long result = hostGroupService.getHostCountByGroupId(groupId);
        return Response.success(result);
    }
}